import sys, urllib 
from HTMLParser import HTMLParser

#Analyze the web page        
class Analyzer(HTMLParser):
    tags={'title':''}
    current='none'
    
    def analyze(self,data):
        print 'analyze data'
        self.feed(data)
        self.close()
        
    def handle_starttag(self, tag, attrs):
        if(tag.lower()=='title'):
            self.start_title(attrs)
        elif(tag.lower()=='meta'):
            self.start_meta(attrs)
        elif(tag.lower()=='a'):
            self.start_a(attrs)
            
            
    def handle_endtag(self, tag):
        self.current='none'
            
    def start_title(self, attrs):
        self.current='title'
        
    def end_title(self):
        self.current='none'
        
    #Find the charset    
    def start_meta(self,attrs):
        for str in attrs:
            print str
    
    #Find the links
    def start_a(self,attrs):
        self.current='hyperlinks'
        self.tags['hyperlinks']+=attrs['href']
        
            
    def handle_data(self,text):
        if self.current=='none':
            return
        if(self.tags.__contains__(self.current)):
            self.tags[self.current]+=text #TODO: Make this string processing more efficient
        else:
            self.tags['text']+=text   
            
#Retrieve the web page and parse the html tags. Extract the useful information
class WebCrawler: 
    def __init__(self,analyzer):
        self.m_analyzer=analyzer
        self.m_tags={'title':'', 'encoding':'','language':'', 'hyperlinks':'','table':'','text':''} 

    def retrieve(self, url):
        reader=urllib.urlopen(url)
        data=reader.read()
        reader.close()
        f=open('text','w')
        f.write(data)
        f.close()
        return data


def main():
    print 'main'
    wc=WebCrawler(Analyzer())
    wc.retrieve('http://www.javaeye.com')
    

if __name__=='__main__':
    main()